package com.augurit.gzcgwsp.ljzyz.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.common.util.CommonUtils;
import com.augurit.gzcgwsp.jzfqwczz.convert.YwYnJzfqwczcJbxxConvertor;
import com.augurit.gzcgwsp.ljzyz.convert.YwYnLjzyzSqbConvertor;
import com.augurit.gzcgwsp.ljzyz.dao.YwYnLjzyzSqbDao;
import com.augurit.gzcgwsp.ljzyz.entity.YwYnBusinfo;
import com.augurit.gzcgwsp.ljzyz.entity.YwYnLjzyzSqb;
import com.augurit.gzcgwsp.ljzyz.service.IYwYnLjzyzSqbService;
import com.augurit.gzcgwsp.ljzyz.web.form.YwYnLjzyzSqbForm;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjclForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistForm;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.app.doccode.web.form.OaDocCodeForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

@Service
@Transactional
public class YwYnLjzyzSqbServiceImpl extends WfBusClientServiceImpl<YwYnLjzyzSqbForm> implements IYwYnLjzyzSqbService{

	@Autowired
	private YwYnLjzyzSqbDao ywYnLjzyzSqbDao;
    @Autowired
    private IYwComQyxxService ywComQyxxService;
    @Autowired
    private IYwComSjclService sjclService;
	@Autowired
    private IOaDocCodeService doccodeService;
	@Autowired
    private IYwComSjcllistService ywComSjcllistService;
    @Autowired
    private IYwComSjclService ywComSjclService;
    
	@Transactional(readOnly = true)
	public YwYnLjzyzSqbForm get(Long id) {
		YwYnLjzyzSqb entity = ywYnLjzyzSqbDao.get(id);
		YwYnLjzyzSqbForm form=YwYnLjzyzSqbConvertor.convertVoToForm(entity);
        YwComSjcllistForm sjclListForm=new YwComSjcllistForm();
        YwComQyxxForm qyxxform = ywComQyxxService.get(form.getQyid());
        sjclListForm.setAjid(id);
        sjclListForm.setTplcode("jzljzyz_xzfz");
        List<YwComSjcllistForm> sjclList = ywComSjcllistService.getList(sjclListForm);
        YwComSjclForm fkForm = new YwComSjclForm();
        fkForm.setFkid(form.getQyid());
        fkForm.setAjid(id);
        fkForm.setTplcode("jzljzyz_xzfz");
        List<YwComSjclForm> sjclTpl = ywComSjclService.getList(fkForm);
        for (YwComSjcllistForm listForm : sjclList) {
            boolean hasExists = false;
            for (YwComSjclForm tplForm : sjclTpl) {
                if (tplForm.getTplid() != null && listForm.getId().longValue() == tplForm.getTplid().longValue()) {
                    if (StringUtils.isNotBlank(tplForm.getFilepath())) {
                        listForm.setState("1");
                        listForm.setHasfile("1");
                        listForm.setFilename(tplForm.getFilename());
                        listForm.setFilepath(tplForm.getFilepath());
                        listForm.setFiletype(tplForm.getFiletype());
                    } else {
                        listForm.setState("0");
                        listForm.setHasfile("0");
                    }
                    if (tplForm.getYjs() != null) {
                        listForm.setYjs(Long.valueOf(tplForm.getYjs()));
                    }
                    if (tplForm.getFyjs() != null) {
                        listForm.setFyjs(Long.valueOf(tplForm.getFyjs()));
                    }
                    listForm.setClid(tplForm.getId());
                    if ("1".equals(listForm.getSfdz())) {
                        listForm.setDisabled("1");
                    } else {
                        listForm.setDisabled("0");
                        if (tplForm.getSfdz() == null) {
                            listForm.setSfdz("0");
                        } else {
                            listForm.setSfdz(tplForm.getSfdz());
                        }
                    }
                    hasExists = true;
                }
            }
            if (!hasExists) {
                listForm.setState("0");
                listForm.setHasfile("0");
                if ("1".equals(listForm.getSfdz())) {
                    listForm.setDisabled("1");
                } else {
                    listForm.setDisabled("0");
                }
            }
        }
        form.setAcceptNo2(form.getAcceptNo());
        form.setCbyw2(form.getCbyw());
        form.setCbyw3(form.getCbyw());
        form.setCbyw4(form.getCbyw());
        form.setSjr2(form.getSjr());
        form.setSqdw2(form.getSqdw());
        form.setSajg2("广州市城市管理委员会");
        form.setSajg(form.getSajg2());
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        YwYnLjzyzSqbConvertor.convertQyxxFormToForm(qyxxform, form);
        if(sjclList.size()>0)
        {
                YwComSjcllistForm sjclForm01=sjclList.get(0);
                form.setTitleRow101(sjclForm01.getTitle());
                form.setTitleRow102(sjclForm01.getTitle());
                form.setFyjsRow101(sjclForm01.getFyjs());
                form.setFyjsRow102(sjclForm01.getFyjs());
                form.setFsRow101(sjclForm01.getYjs()+sjclForm01.getFyjs());
                form.setFsRow102(sjclForm01.getYjs()+sjclForm01.getFyjs());
                if("0".equals(sjclForm01.getSfdz()))
                {
                    form.setSfdzRow101("");
                    form.setSfdzRow102("");
                }else{
                    form.setSfdzRow101("√");
                    form.setSfdzRow102("√");
                }
                
                YwComSjcllistForm sjclForm02=sjclList.get(1);
                form.setTitleRow201(sjclForm02.getTitle());
                form.setTitleRow202(sjclForm02.getTitle());
                form.setFyjsRow201(sjclForm02.getFyjs());
                form.setFyjsRow202(sjclForm02.getFyjs());
                form.setFsRow201(sjclForm02.getYjs()+sjclForm02.getFyjs());
                form.setFsRow202(sjclForm02.getYjs()+sjclForm02.getFyjs());
                if("0".equals(sjclForm02.getSfdz()))
                {
                    form.setSfdzRow201("");
                    form.setSfdzRow202("");
                }else{
                    form.setSfdzRow201("√");
                    form.setSfdzRow202("√");
                }
                
                YwComSjcllistForm sjclForm03=sjclList.get(2);
                form.setTitleRow301(sjclForm03.getTitle());
                form.setTitleRow302(sjclForm03.getTitle());
                form.setFyjsRow301(sjclForm03.getFyjs());
                form.setFyjsRow302(sjclForm03.getFyjs());
                form.setFsRow301(sjclForm03.getYjs()+sjclForm03.getFyjs());
                form.setFsRow302(sjclForm03.getYjs()+sjclForm03.getFyjs());
                if("0".equals(sjclForm03.getSfdz()))
                {
                    form.setSfdzRow301("");
                    form.setSfdzRow302("");
                }else{
                    form.setSfdzRow301("√");
                    form.setSfdzRow302("√");
                }
        }
        form.setLxr2(form.getYwlxr());
        if(form.getSqrq() != null)
            form.setSqrq2(format.format(form.getSqrq()));
        if(form.getLarq() != null)
            form.setLarq2(format.format(form.getLarq()));
        return form;
		
		//return YwYnLjzyzSqbConvertor.convertVoToForm(entity);
	}
	
	/**
	 * ɾ��Form�����б�
	 */
	public void delete(Long... ids) {
		ywYnLjzyzSqbDao.delete(ids);
	}

	/**
	 * �����������޸ĵ�Form�����б�
	 */
	public void save(YwYnLjzyzSqbForm... forms) {
		if(forms != null)
			for(YwYnLjzyzSqbForm form : forms)
				this.save(form);
	}
	
	/**
	 * �����������޸ĵ�Form����
	 */
	public void save(YwYnLjzyzSqbForm form){
		
		if(form != null){
			YwYnLjzyzSqb entity = null;
			OaDocCodeForm doccodeForm = doccodeService.get(10L);
			if(form != null && form.getId() != null){
				entity = ywYnLjzyzSqbDao.get(form.getId());
				if (StringUtils.isBlank(entity.getAcceptNo()) || form.getAcceptNo().indexOf("XXXX") > 0) form.setAcceptNo(null);
			}else{
				entity = new YwYnLjzyzSqb();
                form.setAcceptNo(null);
			}
			boolean isNewForm = false;
            if (StringUtils.isBlank(form.getAcceptNo())) {
                String yymmdd = DateUtils.dateTimeToString(new Date(), DateUtils.YYYYMMDD);
                String seq = doccodeForm.getSeq() + "";
                
                if (seq.length() == 1) seq = "00000" + seq;
                else if (seq.length() == 2) seq = "0000" + seq;
                else if (seq.length() == 3) seq = "000" + seq;
                else if (seq.length() == 4) seq = "00" + seq;
                else if (seq.length() == 5) seq = "0" + seq;
                
                String acceptNo = yymmdd.substring(2) + seq;
                form.setAcceptNo(acceptNo);
                System.out.println("acceptNo ====================== "+acceptNo);
                doccodeService.updateForAddSeq(doccodeForm.getId());
                isNewForm = true;
            }
            
            YwComQyxxForm qyxxForm = new YwComQyxxForm();
            YwYnLjzyzSqbConvertor.convertFormToQyxxForm(form, qyxxForm);
            ywComQyxxService.update(qyxxForm);
            
            form.setQyid(qyxxForm.getId());
//            boolean isNewForm = false;
//            if (StringUtils.isBlank(form.getAcceptNo())) {
//                form.setAcceptNo(CommonUtils.convertSeqToAcceptNo(doccodeForm.getSeq()));
//                doccodeService.updateForAddSeq(doccodeForm.getId());
//                isNewForm = true;
//            }
            YwYnLjzyzSqbConvertor.convertFormToVo(form, entity);
			
			//����
			ywYnLjzyzSqbDao.save(entity);
			form.setId(entity.getId());
			if (isNewForm) {
			    sjclService.updateByNewForm(form.getId());
			}
			
		}
	}

	/**
	 * ���Form����Ĳ�ѯ��������ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<YwYnLjzyzSqbForm> search(Page<YwYnLjzyzSqbForm> page, YwYnLjzyzSqbForm form) {
		// �����µķ�ҳ����
		Page pg = PageUtils.newPageInstance(page);
		
		// ��ѯ��估����
		StringBuffer hql = new StringBuffer("from YwYnLjzyzSqb ps where 1=1");
		List values = new ArrayList();
		
		// ��ѯ����
		if(form != null){
			
		}
		
		//����
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// ִ�з�ҳ��ѯ����
		ywYnLjzyzSqbDao.findPage(pg, hql.toString(), values);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<YwYnLjzyzSqbForm> list = YwYnLjzyzSqbConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * ��ݹ��������б�����ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<YwYnLjzyzSqbForm> search(Page<YwYnLjzyzSqbForm> page, List<PropertyFilter> filters) {
		// �����µķ�ҳ����
		Page<YwYnLjzyzSqb> pg = PageUtils.newPageInstance(page);
		
		// ������������ҳ���Ҷ���
		ywYnLjzyzSqbDao.findPage(pg, filters);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<YwYnLjzyzSqbForm> list = YwYnLjzyzSqbConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
    public void changeYslState(Long id, Long state) {
        YwYnLjzyzSqb entity = ywYnLjzyzSqbDao.get(id);
        ywYnLjzyzSqbDao.save(entity);
    }

    /*
     * 查询车辆信息
     * @see com.augurit.gzcgwsp.ljzyz.service.IYwYnLjzyzSqbService#getCars(com.augurit.gzcgwsp.ljzyz.web.form.YwYnLjzyzSqbForm)
     */
	public List<YwYnBusinfo> getCars(YwYnLjzyzSqbForm form) {
		String sql=" from YwYnBusinfo"+" where qyid="+form.getQyid();
		return ywYnLjzyzSqbDao.find(sql);
	}
}
